package com.th.therp.mapper;

import com.github.pagehelper.Page;
import com.th.therp.pojo.dto.page.SalePageQueryDTO;
import com.th.therp.pojo.emtity.Sale;
import com.th.therp.pojo.vo.DashboardMonthVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface SaleMapper {

    @Insert("insert into sale (barcode, store, customer, material, quantity, total_amount, operator, create_time,is_deleted) VALUES (#{barcode},#{store},#{customer},#{material},#{quantity},#{totalAmount},#{operator},#{createTime},0)")
    void add(Sale sale);

    Page<Sale> page(SalePageQueryDTO salePageQueryDTO);

    @Select("select sum(quantity) as quantity,sum(total_amount) as total_amount from sale where create_time>#{startOfDay} and create_time<#{endOfDay} and is_deleted=0")
    DashboardMonthVo findDashboardMonthVoByTime(String startOfDay, String endOfDay);

    // 根据时间范围查找销售订单的数量
    @Select("select sum(quantity) as quantity from sale where create_time between #{startTime} and #{endTime} and is_deleted=0")
    Integer findQuantityByTime(String startTime, String endTime);

    // 根据时间范围查找销售订单的金额
    @Select("select sum(total_amount) as total_amount from sale where create_time between #{startTime} and #{endTime} and is_deleted=0")
    Double findTotalAmountByTime(String startTime, String endTime);

    // 根据id查询销售单
    @Select("select * from sale where id=#{id}")
    Sale findById(Integer id);

    //根据id删除数据(退货)
    @Delete("update sale set is_deleted=1 where id=#{id}")
    void delete(Integer id);
}
